;======================================================================
;页文本列表项显示位置
Page_List_Item_Pos
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*00 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*01 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*02 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*03 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*04 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*05 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*06 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*07 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*08 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*09 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*10 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*11 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*12 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*13 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*14 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*15 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*16 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*17 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*18 + 0)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*19 + 0)

    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*00 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*01 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*02 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*03 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*04 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*05 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*06 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*07 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*08 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*09 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*10 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*11 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*12 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*13 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*14 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*15 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*16 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*17 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*18 + 6)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*19 + 6)
    
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*00 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*01 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*02 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*03 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*04 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*05 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*06 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*07 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*08 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*09 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*10 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*11 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*12 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*13 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*14 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*15 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*16 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*17 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*18 + 12)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*19 + 12)
    
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*00 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*01 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*02 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*03 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*04 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*05 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*06 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*07 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*08 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*09 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*10 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*11 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*12 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*13 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*14 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*15 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*16 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*17 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*18 + 18)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*19 + 18)
    
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*00 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*01 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*02 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*03 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*04 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*05 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*06 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*07 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*08 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*09 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*10 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*11 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*12 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*13 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*14 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*15 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*16 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*17 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*18 + 24)
    .DW LIST_ITEM_POS+(($20*(LIST_ITEM_LINE_SPACING+1))*19 + 24)
    
;======================================================================
;写入文本PPU位置
Page_List_Item_Pos_Write
    LDA FC_Page_Item_Index
    ASL A
    TAY
    LDA Page_List_Item_Pos + 1,Y
    STA FC_PPU_Buf_Addr,X
    INX
    LDA Page_List_Item_Pos + 0,Y
    STA FC_PPU_Buf_Addr,X
    INX
    RTS

;======================================================================
;页文本列表项清除
Page_List_Item_Clear
    LDA FC_Page_Item_Index
    CLC
    ADC FC_Page_Index_Last_L
    STA FC_Temp_Src_Index_L
    
    LDA FC_Item_Count_H
    ADC FC_Page_Index_Last_H
    STA FC_Temp_Src_Index_H
    
    LDA #<ITEM_INDEX_MAX
    STA FC_Temp_Dest_Index_L
    LDA #>ITEM_INDEX_MAX
    STA FC_Temp_Dest_Index_H
    
    JSR FC_Cmp_Item_Index
    BEQ .Start
    BCS .End

.Start

    LDX FC_PPU_Buf_Count
    
    ;写入行模式
    LDA #FC_PPU_MODE_CLEAR
    STA FC_PPU_Buf_Addr,X
    INX
    
    JSR Page_List_Item_Pos_Write
    
    LDA #$05
    STA FC_PPU_Buf_Addr,X
    INX
    
    STX FC_PPU_Buf_Count
.End
    RTS

;======================================================================
;检查项是否为焦点项
Page_List_Item_Hover
    LDA #$00
    STA FC_Char_Offset
    
    LDA FC_Page_Item_Index
    CLC
    ADC FC_Page_Index_L
    STA FC_Temp_Src_Index_L
    LDA #$00
    ADC FC_Page_Index_H
    STA FC_Temp_Src_Index_H

    LDA FC_Item_Index_L
    STA FC_Temp_Dest_Index_L
    LDA FC_Item_Index_H
    STA FC_Temp_Dest_Index_H
    JSR FC_Cmp_Item_Index
    BNE .End
    
    LDA #$80
    STA FC_Char_Offset
    
.End

    RTS

;======================================================================
Page_Item_Upate_Active
    SEC
    LDA FC_Item_Index_L
    SBC FC_Page_Index_L
    STA FC_Page_Item_Index

    LDA FC_Char_Offset
    PHA
    LDA #$80
    STA FC_Char_Offset
    JSR Page_List_Item_Write
    PLA
    STA FC_Char_Offset

    RTS
    
;======================================================================
Page_Item_Upate_No_Active
    SEC
    LDA FC_Item_Index_L
    SBC FC_Page_Index_L
    STA FC_Page_Item_Index

    LDA FC_Char_Offset
    PHA
    LDA #$00
    STA FC_Char_Offset
    JSR Page_List_Item_Write
    PLA
    STA FC_Char_Offset

    RTS
    
;======================================================================
;页文本列表项写入
Page_List_Item_Write

    LDA FC_Page_Item_Index
    CLC
    ADC FC_Page_Index_L
    STA FC_Temp_Src_Index_L
    
    LDA FC_Item_Count_H
    ADC FC_Page_Index_H
    STA FC_Temp_Src_Index_H
    BCS .End
    
    LDA #<ITEM_INDEX_MAX
    STA FC_Temp_Dest_Index_L
    LDA #>ITEM_INDEX_MAX
    STA FC_Temp_Dest_Index_H
    
    JSR FC_Cmp_Item_Index
    BEQ .Start
    BCS .End

.Start
    LDX FC_PPU_Buf_Count
    
    ;写入行模式
    LDA #FC_PPU_MODE_LINE
    STA FC_PPU_Buf_Addr,X
    INX
    
    JSR Page_List_Item_Pos_Write
    
    ;写入序号
    LDA #$05
    STA FC_PPU_Buf_Addr,X
    INX
    
    LDA FC_Page_Item_Index
    CLC
    ADC FC_Page_Index_L
    STA FC_Temp_Dest_Index_L
    
    LDA FC_Item_Count_H
    ADC FC_Page_Index_H
    STA FC_Temp_Dest_Index_H
    
    LDA FC_Temp_Dest_Index_L
    CLC
    ADC #$01
    STA FC_HEX_Data_L
    LDA FC_Temp_Dest_Index_H
    ADC #$00
    STA FC_HEX_Data_H
    
    JSR FC_HEX_To_Dec_16
    JSR FC_Convert_Dec_To_PPU_10000
    
    STX FC_PPU_Buf_Count
    
.End

    RTS

;======================================================================
;页文本列表更新
Page_List_Update
    LDA FC_NMI_Task_Flag
    AND #FC_NMI_TASK_PAGING
    BNE .End
    
    ;标记翻页进行中
    LDA FC_NMI_Task_Flag
    ORA #FC_NMI_TASK_PAGING
    STA FC_NMI_Task_Flag
    
    LDA FC_Char_Offset
    PHA
    
    LDA #$00
    STA FC_Page_Item_Index
    STA FC_Page_Item_Buffer
    
    ;PPU 缓冲还有数据则处理完
    LDX FC_PPU_Buf_Count
    BEQ .Update_Item_Content
    
    JSR .Page_Buffer_Flush
    
.Update_Item_Content

    JSR Page_List_Item_Clear
    
    JSR Page_List_Item_Hover
    JSR Page_List_Item_Write

    INC FC_Page_Item_Index
    INC FC_Page_Item_Buffer
    
    LDA FC_Page_Item_Buffer
    CMP #ITEM_PAGE_BUFFER_SIZE
    BCC .Update_Item_Content_Continue
    
    ;刷新缓冲
    JSR .Page_Buffer_Flush
    
.Update_Item_Content_Continue
    LDA FC_Page_Item_Index
    CMP #ITEM_PAGE_SIZE
    BCC .Update_Item_Content

.Update_Item_Finish
    LDA FC_Page_Item_Buffer
    BEQ .Page_List_Update_End
    JSR .Page_Buffer_Flush
.Page_List_Update_End
    PLA
    STA FC_Char_Offset
    
    LDA FC_NMI_Task_Flag
    AND #FC_NMI_TASK_PAGING ^ $FF
    STA FC_NMI_Task_Flag
    
.End

    RTS

;刷新 PPU 缓冲(等待 NMI 处理完缓冲)
.Page_Buffer_Flush
    LDA #$00
    STA FC_Page_Item_Buffer
    LDA FC_NMI_Task_Flag
    ORA #FC_NMI_TASK_BUF_READY
    STA FC_NMI_Task_Flag
.Wait_Vblank
    LDA FC_NMI_Task_Flag
    AND #FC_NMI_TASK_BUF_READY
    BNE .Wait_Vblank
    RTS
    